/**===================================================================================================================
 *          Author:qinyu
 *          Date:  2012-06-18
 *          Description: The file package Http Get,Post,Put,Delete asynchronized request.
 *====================================================================================================================*/

/**
 *The ajax request error exception.
 */
function BusinessProcessException() {
    this.showMessage = function (httpCode, errorInfo) {
        switch (parseInt(httpCode)) {
            case 400:{
                $("#error-block").html(errorInfo.message);
                break;
            }
            default:
            {
                $("#error-block").html(errorInfo.message);
                break;
            }
        }
    }
}

/**
 *The class define send Asynchronized http request.
 */

function Asynchronized(url, httpMethod, data, successCallBack, errorCallBack) {
    this.send = function () {
        $("#error-block").html(""); //use to clean error message in each page
        var entry = {type:httpMethod, url:url, contentType:"application/json", dataType:"json", cache:false, processData:false, success:successCallBack};
        if (data != null)entry.data = data;

        entry.success = function(json){
            if((null != json) && (typeof(json.sessionTimeout) != "undefined") ){
                if(json.sessionTimeout){
                    window.location.href=createUrl("/login");
                    return;
                }
            }//execute session time out.
            successCallBack(json);
        }

        entry.error = function (XMLHttpRequest) {
            var httpCode = XMLHttpRequest.status;
            var errorInfo = $.parseJSON(XMLHttpRequest.responseText);
            if (typeof(errorCallBack) != "undefined") {
                errorCallBack(httpCode, errorInfo);
            }
            else {
                var ex = new BusinessProcessException();
                ex.showMessage(httpCode, errorInfo);
            }
        }
        $.ajax(entry);
    }
}

/**
 *The class use create url and send data.
 */
function HttpClient(url, data) {
    this.execute = function (http) {
        http.execute(url, ((typeof(data) != "undefined") && (data != null)) ? JSON.stringify(data) : null);
    }
}

/**
 *This is abstract class,define http request.
 */
function Request(requestType, successCallBack, errorCallBack) {
    this.execute = function (url, data) {
        var request = new Asynchronized(url, requestType, data, successCallBack, errorCallBack);
        request.send();
    }
}

/**
 *The method is package Get request extends Request
 */
function Get(successCallBack, errorCallBack) {
    Request.call(this, "GET", successCallBack, errorCallBack);
}
Get.prototype = new Request();

/**
 *The method is package Post request extends Request
 */
function Post(successCallBack, errorCallBack) {
    Request.call(this, "POST", successCallBack, errorCallBack);
}
Post.prototype = new Request();

/**
 *The method is package Delete request extends Request
 */
function Delete(successCallBack, errorCallBack) {
    Request.call(this, "DELETE", successCallBack, errorCallBack);
}
Delete.prototype = new Request();

/**
 *The method is package Put request extends Request
 */
function Put(successCallBack, errorCallBack) {
    Request.call(this, "PUT", successCallBack, errorCallBack);
}
Put.prototype = new Request();
